package com.gz.moreaseManager.modules.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gz.moreaseManager.config.SelfUserDetails;
import com.gz.moreaseManager.core.modal.AjaxResult;
import com.gz.moreaseManager.core.modal.HttpCode;
import com.gz.moreaseManager.core.modal.PageResult;
import com.gz.moreaseManager.modules.entity.Store;
import com.gz.moreaseManager.modules.service.IStoreService;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.gz.moreaseManager.core.base.BaseController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 门店信息 前端控制器
 * </p>
 *
 * @author wike
 * @since 2019-02-26
 */
@RestController
@RequestMapping("/store")
public class StoreController extends BaseController {

    @Autowired
    private IStoreService storeService;

    @RequestMapping("list")
    public ModelAndView list(){
        return new ModelAndView("modules/store/list");
    }

    @GetMapping("listPage")
    public PageResult<Store> findPageLsit(String name,String tel, String proName,String cityName,
                                          Integer pageNum, Integer pageSize){
        //分页
        Page paramsPage = new Page();
        paramsPage.setCurrent(pageNum);
        paramsPage.setSize(pageSize);
        //构建查询
        Wrapper wrapper = new EntityWrapper<Store>();
        if( UtilTool.isNotBlank( name ))
            wrapper.like("name",name);
        if( UtilTool.isNotBlank( tel ))
            wrapper.like("tel",tel);
        if( UtilTool.isNotBlank( proName ))
            wrapper.eq("pro_name",proName);
        if( UtilTool.isNotBlank( cityName ))
            wrapper.eq("city_name",cityName);

        wrapper.orderBy("create_time",false);
        Page<Store> data = storeService.selectPage(paramsPage,wrapper);
        return new PageResult(0, null, data.getTotal(), data.getRecords());

    }

    /**
     * 新增门店
     * @param store
     * @return
     */
    @PostMapping("addStore")
    public AjaxResult<Boolean> addStore(Store store,Authentication authentication){
        if( store == null
                || UtilTool.isBlank( store.getName())
                || UtilTool.isBlank( store.getTel())
                || UtilTool.isBlank( store.getAddr())
                || UtilTool.isBlank( store.getCityName())
                || UtilTool.isBlank( store.getProName()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        store.setCreateTime(new Date());
        //创建人
        SelfUserDetails userDetails = (SelfUserDetails) authentication.getPrincipal();
        store.setCreatePerson( userDetails.getUsername());
        Boolean success = storeService.insert(store);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "新增门店信息失败!");
    }

    /**
     * 获取门店信息
     * @param id
     * @return
     */
    @GetMapping("findStore")
    public AjaxResult<Store> findStore( Integer id ){
        if( UtilTool.isBlank( id ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Store store = storeService.selectById( id );
        return AjaxResult.success(store);

    }

    /**
     * 修改门店
     * @param store
     * @return
     */
    @PostMapping("editStore")
    public AjaxResult<Boolean> editStore( Store store ){
        if( store == null
                || UtilTool.isBlank( store.getId())
                || UtilTool.isBlank( store.getName())
                || UtilTool.isBlank( store.getTel())
                || UtilTool.isBlank( store.getAddr())
                || UtilTool.isBlank( store.getCityName())
                || UtilTool.isBlank( store.getProName()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Boolean success = storeService.updateById(store);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "修改门店信息失败!");
    }

    /**
     * 根据ID删除门店信息
     * @param id
     * @return
     */
    @PostMapping("delete")
    public AjaxResult<Boolean> delete( Integer id){

        if( UtilTool.isBlank( id ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Boolean success = storeService.deleteById( id );
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "删除门店信息失败!");
    }

    /**
     * 获取门面版图信息
     * @param proName
     * @param cityName
     * @return
     */
    @GetMapping("findStoreList")
    public AjaxResult<List<Store>> findStoreList(String proName ,String cityName,String name ){

        Wrapper wrapper = new EntityWrapper<Store>();
        if( UtilTool.isNotBlank( proName ))
            wrapper.eq("pro_name",proName);
        if( UtilTool.isNotBlank( cityName ))
            wrapper.eq("city_name",cityName);
        if( UtilTool.isNotBlank( name ))
            wrapper.like("addr",name).or().like("name",name);

        wrapper.orderBy("create_time",false);
        List<Store> list = storeService.selectList(wrapper);
        if( UtilTool.isNotBlank( list ))
            return AjaxResult.success( list ) ;
        return AjaxResult.build(HttpCode.FAILED, "获取门面版图信息失败!");
    }
}

